﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#include "WallTagSnapStrategy.h"
#include "IModelView.h"
#include "IElementSnapComponent.h"
#include "WallTag.h"
#include "ElementSnapStrategyRegister.h"
#include "IGenericElement.h"
#include "ILine3d.h"
#include "EnableCompileWarning_The_LAST_IncludeInCpp.h"

using namespace gcmp;
using namespace Sample;

REGISTER_ELEMENT_SNAP_STRATEGY(WallTag, WallTagSnapStrategy);

std::vector<gcmp::RemoteSnapData> Sample::WallTagSnapStrategy::GetRemoteSnappableGeometry(const IModelView* pModelView, const IElement* pElement) const
{
    std::vector<gcmp::RemoteSnapData> opSnap;
    DBG_WARN_AND_RETURN_UNLESS(pElement, opSnap, L"pElement为空",L"GDMP",L"2023-12-30");
    const IGenericElement* pGenericElement = quick_cast<const IGenericElement>(pElement);
    DBG_WARN_AND_RETURN_UNLESS(pGenericElement, opSnap, L"pGenericElement为空",L"GDMP",L"2023-12-30");
    const IExternalObject* pExternalObject = pGenericElement->GetExternalObject();
    DBG_WARN_AND_RETURN_UNLESS(pExternalObject, opSnap, L"pExternalObject为空",L"GDMP",L"2023-12-30");
    const WallTag* pWallTag = quick_cast<const WallTag> (pExternalObject);
    DBG_WARN_AND_RETURN_UNLESS(pWallTag, opSnap, L"pWallTag为空",L"GDMP",L"2023-12-30");
    Vector3d referenceLineStartPoint = pWallTag->GetReferenceLineStartPoint();
    OwnerPtr<ILine3d> opCurve3d = ILine3d::Create(referenceLineStartPoint, referenceLineStartPoint + pWallTag->GetReferenceLineDir());
    opSnap.emplace_back(gcmp::RemoteSnapData(TransferOwnershipCast<IGeometry>(opCurve3d), 0));
    return opSnap;
}

void Sample::WallTagSnapStrategy::GetLocalCustomPointSnapData(const gcmp::IElement* pElement, const gcmp::IModelView* pModelView, std::vector<gcmp::LocalCustomPointSnapData>& snapDatas) const
{
    DBG_WARN_AND_RETURN_VOID_UNLESS(pModelView, L"pModelView为空",L"GDMP",L"2023-12-30");
    DBG_WARN_AND_RETURN_VOID_UNLESS(pElement, L"pElement为空",L"GDMP",L"2023-12-30");
    const IGenericElement* pGenericElement = quick_cast<const IGenericElement>(pElement);
    DBG_WARN_AND_RETURN_VOID_UNLESS(pGenericElement, L"pGenericElement为空",L"GDMP",L"2023-12-30");
    const IExternalObject* pExternalObject = pGenericElement->GetExternalObject();
    DBG_WARN_AND_RETURN_VOID_UNLESS(pExternalObject, L"pExternalObject为空",L"GDMP",L"2023-12-30");
    const WallTag* pWallTag = quick_cast<const WallTag> (pExternalObject);
    DBG_WARN_AND_RETURN_VOID_UNLESS(pWallTag, L"pWallTag为空",L"GDMP",L"2023-12-30");
    Vector3d referenceLineStartPoint = pWallTag->GetReferenceLineStartPoint();
    Vector3d referenceLineDir = pWallTag->GetReferenceLineDir();
    // 二维视图
    std::vector<GraphicsNodeId> grepPath;
    if (BuiltInViewType::TwoDimensional == pModelView->GetViewType())
    {
        LocalCustomPointSnapData data = LocalCustomPointSnapData(SnapTypeForCustomPoint::CustomPoint, referenceLineStartPoint + referenceLineDir, L"墙标注拖拽点", grepPath, L"");
        snapDatas.emplace_back(data);
    }
    // 三维视图
    else if (BuiltInViewType::ThreeDimensional == pModelView->GetViewType())
    {
        LocalCustomPointSnapData data1 = LocalCustomPointSnapData(SnapTypeForCustomPoint::CustomPoint, referenceLineStartPoint + referenceLineDir, L"墙标注拖拽点", grepPath, L"");
        snapDatas.emplace_back(data1);
    }
}
